﻿package com.lbynet.ui
{
	
	import flash.display.*;
	import flash.events.*;
	
	import lby.events.*;
	import caurina.transitions.*;
	
	/**
	 * ...
	 * @author lbynet (Tools -> 简单滚动...)
	 */
	public class ScrollBarY 
	{
		
		private var yOffset:Number;
		private var yMin:Number = 0;
		private var yMax:Number;
		
		private var scrollbarY_mc:*;
		private var mask_mc:*;
		private var content_mc:*;
		private var mainStage:*;
		private var Parent:*;
		
		private static var Instance:ScrollBarY = new ScrollBarY();
		
		public function ScrollBarY() 
		{
			if(Instance!=null){
				throw new Error("单件")
			}
		}
		
		/*
		 * 单件类的实例 
		*/
		public static function getInstance():ScrollBarY {
			
			return Instance;
		}
		
		public function setScrollBarY(mask_mc:*,content_mc:*,scrollbarY_mc:*,stage:*,Parent:*):void {
			
			this.mask_mc = mask_mc;
			this.content_mc = content_mc;
			this.scrollbarY_mc = scrollbarY_mc;
			this.mainStage = stage;
			this.Parent = Parent
			
			this.content_mc.mask = this.mask_mc;
			
			init();
			
		}
		private function init():void {
			
			yMax = this.scrollbarY_mc.track.height - this.scrollbarY_mc.thumb.height;
			
			this.scrollbarY_mc.thumb.buttonMode = true;
			this.scrollbarY_mc.thumb.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			this.scrollbarY_mc.addEventListener(MouseEvent.MOUSE_UP, upHandler);
			
			//使用了自己定义的事件类,下面两行是自己定义的事件类的应用
			SimpleMouseEventHandler.register(this.scrollbarY_mc.thumb); //传入要作为侦听的对象
			this.scrollbarY_mc.thumb.addEventListener(SimpleMouseEvent.RELEASE_OUTSIDE, releaseHandler);
			
			
			
			this.Parent.addEventListener(MouseEvent.ROLL_OVER, mouseOverHandler);
			this.Parent.addEventListener(MouseEvent.ROLL_OUT, mouseOutHandler);
			
		}
		
		private function releaseHandler(e:SimpleMouseEvent) {
			
			this.upHandler();
		}
		
		
		private function downHandler(e:MouseEvent):void {
			
			this.mainStage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
			yOffset = this.mainStage.mouseY - this.scrollbarY_mc.thumb.y;
		}
		private function upHandler(e:MouseEvent = null):void {
			
			this.mainStage.removeEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
		}
		
		private function moveHandler(event:MouseEvent = null):void {
			
			this.scrollbarY_mc.thumb.y = this.mainStage.mouseY - yOffset;
			if (this.scrollbarY_mc.thumb.y <= yMin) {
				
				this.scrollbarY_mc.thumb.y = yMin;
			}
			if (this.scrollbarY_mc.thumb.y >= yMax) {
				
				this.scrollbarY_mc.thumb.y = yMax;
			}
			var sp:Number = this.scrollbarY_mc.thumb.y / yMax;
			Tweener.addTween(this.content_mc, {y:(-sp*(this.content_mc.height-this.mask_mc.height)),time:1});
			event.updateAfterEvent();
		}
		
		
		
		
		
		
		
		private function mouseOverHandler(event:MouseEvent):void {
			
			this.mainStage.addEventListener(MouseEvent.MOUSE_WHEEL, wheelHandler);
		}
		private function mouseOutHandler(event:MouseEvent):void {
			//trace("mouseOutHandler");
			this.mainStage.removeEventListener(MouseEvent.MOUSE_WHEEL, wheelHandler);
		}
		
		private function wheelHandler(event:MouseEvent):void {
			//trace(event.delta);
			var wheelspeed:int = event.delta as int;
			if (wheelspeed < 0) {
				
				this.wheelDownHandler(Math.abs(wheelspeed));
			}else {
				
				this.wheelUpHandler(Math.abs(wheelspeed));
			}
			
		}
		
		public function wheelUpHandler(wheelspeed:int):void {
			
			this.scrollbarY_mc.thumb.y -= wheelspeed;
			
			if (this.scrollbarY_mc.thumb.y <= yMin) {
				
				this.scrollbarY_mc.thumb.y=yMin;
			}
			
			var sp:Number = this.scrollbarY_mc.thumb.y / yMax;
			Tweener.addTween(this.content_mc, { y:( -sp * (this.content_mc.height - this.mask_mc.height)), time:1 } );
			
		}
		public function wheelDownHandler(wheelspeed:int):void {
			
			this.scrollbarY_mc.thumb.y += wheelspeed;
			
			if (this.scrollbarY_mc.thumb.y >= yMax) {
				
				this.scrollbarY_mc.thumb.y=yMax;
			}
			
			var sp:Number = this.scrollbarY_mc.thumb.y / yMax;
			Tweener.addTween(this.content_mc, { y:( -sp * (this.content_mc.height - this.mask_mc.height)), time:1 } );
			
		}
		
		
		
		
	}
	
}